#pragma once
#include <assert.h>
#include <iostream>
namespace ssy
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{}

		vector(size_t n, T data = T())
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			resize(n, data);
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		vector(const vector<int>& v)
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			_start = new T[v.capacity()];
			size_t sz = v.size();
			for (int i = 0; i < sz; i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + sz;
			_end_of_storage = _start + v.capacity();
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._e0nd_of_storage);
		}

		vector<T>& operator=(const vector<T> x)
		{
			swap(x);
			return *this;
		}

		~vector()
		{
			if (_start)
			{
				delete _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}


		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _end_of_storage - _start;
		}

		void resize(size_t n, T data = T())
		{
			size_t sz = size();
			if (n < capacity())
			{
				_finish = _start + n;
				for (int i = sz; i < n; i++)
				{
					_start[i] = data;
				}
			}
			else
			{
				reserve(n);
				for (int i = sz; i < n; i++)
				{
					_start[i] = data;
				}
				_finish = _start + n;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				int size = _finish - _start;
				if (_start)
				{
					for (int i = 0; i < size; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + size;
				_end_of_storage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = x;
			++_finish;
		}

		void pop_back()
		{
			--_finish;
		}

		T& operator[](size_t pos)
		{
			assert(pos < capacity());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < capacity());
			return _start[pos];
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		iterator insert(iterator pos, const T& data)
		{
			
			size_t ppos = pos - _start;
			assert(ppos <= size());
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			size_t end = size();
			while (end > ppos)
			{
				_start[end] = _start[end - 1];
				--end;
			}
			_start[end] = data;
			++_finish;

			return _start + ppos;
		}

		iterator erase(iterator pos)
		{
			size_t ppos = pos - _start;
			assert(ppos < size());
			size_t len = ppos;
			size_t  end = size();

			while (ppos < end)
			{
				_start[ppos] = _start[ppos + 1];
				ppos++;
			}

			--_finish;
			return _start + len;
		}

	private:

		T* _start;
		T* _finish;
		T* _end_of_storage;
	};
}